﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net;

namespace WenPostTool.Controls
{
    public partial class PostUserControl : WenSkin.Controls.WenUserControl
    {
        public PostUserControl()
        {
            InitializeComponent();  
        }
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e); 
            comboBox1.Text = "POST";
        }

        #region 委托

        public event PostClickEventHandler PostClick;


        public delegate void PostClickEventHandler(object sender, PostClickEventArgs e);
        public class PostClickEventArgs : EventArgs
        {
            private readonly HistoryItem historyItem;

            public PostClickEventArgs(HistoryItem historyItem)
            {
                this.historyItem = historyItem;
            }

            public HistoryItem HistoryItem { get => historyItem; }
        }
        #endregion

        public void InsertHistoryData(HistoryItem item)
        {
            wenTextBox1.Text = item.Url;
            wenTextBox2.Text = item.Text;
            comboBox1.Text = item.Post;
            wenTextBox3.Text = item.ResultInfo;
        }

        public void OnPostClick()
        {
            string postString = wenTextBox2.Text;
            string url = wenTextBox1.Text;

            HistoryItem historyItem = new HistoryItem()
            {
                Post = comboBox1.Text.ToUpper(),
                DateTime = DateTime.Now,
                Text = postString,
                Url = url,
                ResultInfo=wenTextBox3.Text,
            };
            PostClick?.Invoke(this, new PostClickEventArgs(historyItem));
        }

        private void wenButton1_Click(object sender, EventArgs e)
        {
            switch (comboBox1.Text.ToUpper())
            {
                case "POST":
                    PostClient();
                    break;
                case "GET":
                    GetClient();
                    break;
            }
            OnPostClick();
        }

        private void PostClient()
        {
            string postString = wenTextBox2.Text;
            string url = wenTextBox1.Text;
            if (postString.Length == 0 || url.Length == 0)
                return;


            byte[] postData = Encoding.UTF8.GetBytes(postString);//编码，尤其是汉字，事先要看下抓取网页的编码方式

            WebClient wc = new WebClient();
            wc.Headers.Add("Content-Type", "application/x-www-form-urlencoded");//采取POST方式必须加的header，如果改为GET方式的话就去掉这句话即可
            byte[] responseData = wc.UploadData(url, "POST", postData);//得到返回字符流  
            string srcString = Encoding.UTF8.GetString(responseData);//解码

            wenTextBox3.Text = srcString;
        }
        private void GetClient()
        {
            string url = wenTextBox1.Text;
            if (url.Length == 0)
                return;

            WebClient wc = new WebClient();
            wc.Encoding = Encoding.UTF8;
            string srcString = wc.DownloadString(url);

            wenTextBox3.Text = srcString;
        }
    }
}
